/**
 * Project Name:kany-project-qqgame
 * File Name:LoginFrame.java
 * Package Name:me.kany.project.ui
 * Date:2016年5月3日上午11:21:08
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.project.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import me.kany.utils.config.ini.IniEditor;
import me.kany.utils.image.awt.manager.ImageManager;
import me.kany.utils.swt.button.TransparentButton;
import me.kany.utils.swt.checkbox.TransparentCheckBox;
import me.kany.utils.swt.frame.CenterFrame;
import me.kany.utils.swt.label.LinkLabel;
import me.kany.utils.swt.panel.ImagePanel;

/**
 * ClassName:LoginFrame <br/>
 * Function: 主要的登录窗口. <br/>
 * Date: 2016年5月3日 上午11:21:08 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
@SuppressWarnings("serial")
public class LoginFrame extends CenterFrame {
	/**
	 * 日志信息
	 */

	private static IniEditor iniEditor = new IniEditor();
	private static String style = "default";
	private String frame_title = iniEditor.get("frameSet", "title");
	private Image frame_ico = ImageManager.getImage(iniEditor.get("frameSet", "icon"));
	private ImageIcon logoico = ImageManager.getImageIcon(iniEditor.get("frameSet", "icon"));
	private Font commonTxtFont = new Font("宋体", 0, 12);
	private Color idBordercolor = new Color(82, 186, 255);
	private Color pwdBordercolor = new Color(107, 174, 222);
	private String bgtopc = iniEditor.get(style, "bgtopc");
	private Image topleft = ImageManager.getImage(iniEditor.get(style, "topleft"));
	private Image topright = ImageManager.getImage(iniEditor.get(style, "topright"));
	private TexturePaint topcPaint = ImageManager.createTexturePaint(ImageManager.getImage(this.bgtopc));
	private String bgbottomc = iniEditor.get(style, "bgbottomc");
	private Image bottomleft = ImageManager.getImage(iniEditor.get(style, "bottomleft"));
	private Image bottomright = ImageManager.getImage(iniEditor.get(style, "bottomright"));
	private TexturePaint bottomcPaint = ImageManager.createTexturePaint(ImageManager.getImage(this.bgbottomc));
	private Image mainbg = ImageManager.getImage(iniEditor.get(style, "mainbg"));
	private TexturePaint mainbgPaint = ImageManager.createTexturePaint(this.mainbg);
	private Image bgline = ImageManager.getImage(iniEditor.get(style, "bgline"));
	private TexturePaint bglinePaint = ImageManager.createTexturePaint(this.bgline);
	private Image banner = ImageManager.getImage(iniEditor.get(style, "banner"));
	private Image logginProcessback = ImageManager.getImage(iniEditor.get(style, "logginProcessback"));
	private Image logginProcess = ImageManager.getImage(iniEditor.get(style, "logginProcess"));
	private ImageIcon checkBoxEmptyIcon = ImageManager.getImageIcon(iniEditor.get(style, "checkBoxEmptyIcon"));
	private ImageIcon checkBoxSelectIcon = ImageManager.getImageIcon(iniEditor.get(style, "checkBoxSelectIcon"));
	private ImageIcon checkBoxEmptyRoverIcon = ImageManager.getImageIcon(iniEditor.get(style, "checkBoxEmptyRoverIcon"));
	private ImageIcon checkBoxSelectRoverIcon = ImageManager.getImageIcon(iniEditor.get(style, "checkBoxSelectRoverIcon"));
	private final int topheight = (this.topleft != null) ? this.topleft.getHeight(null) : 29;
	private int width = 354;

	private JButton logoButton = new TransparentButton(this.logoico);
	private JLabel logoText = new JLabel(this.frame_title);
	private ImageIcon advset = ImageManager.getImageIcon(iniEditor.get(style, "advset"));
	private ImageIcon advseth = ImageManager.getImageIcon(iniEditor.get(style, "advseth"));
	private JButton setButton = new TransparentButton(this.advset, this.advseth, this.advset);
	private ImageIcon loginButtonIcon = ImageManager.getImageIcon(iniEditor.get(style, "loginButtonIcon"));
	private ImageIcon loginButtonRoverIcon = ImageManager.getImageIcon(iniEditor.get(style, "loginButtonRoverIcon"));
	private JButton loginButton = new TransparentButton(this.loginButtonIcon, this.loginButtonRoverIcon, this.loginButtonRoverIcon);
	private ImagePanel bannerPanel = new ImagePanel(banner);
	private ImagePanel processPanelDynamic = new ImagePanel(logginProcess);
	private ImagePanel processPanel = new ImagePanel(logginProcessback);

	private final JLabel hintText = new JLabel("请输入帐号和密码：");
	private JLabel userIDLabel = new JLabel("帐号：");
	private JTextField userIDField = new JTextField();
	private JLabel pwdLable = new JLabel("密码：");
	private JPasswordField pwdField = new JPasswordField();
	private JLabel getUserID = new LinkLabel("申请帐号");
	private JLabel getUserPwd = new LinkLabel("忘了密码？");
	private JCheckBox remCheckBox = new TransparentCheckBox(this.checkBoxEmptyIcon, this.checkBoxSelectIcon, this.checkBoxEmptyRoverIcon, this.checkBoxSelectRoverIcon, this.checkBoxEmptyIcon);
	private JCheckBox retreatCheckBox = new TransparentCheckBox(this.checkBoxEmptyIcon, this.checkBoxSelectIcon, this.checkBoxEmptyRoverIcon, this.checkBoxSelectRoverIcon, this.checkBoxEmptyIcon);
	private JCheckBox agreeCheckBox = new TransparentCheckBox(this.checkBoxEmptyIcon, this.checkBoxSelectIcon, this.checkBoxEmptyRoverIcon, this.checkBoxSelectRoverIcon, this.checkBoxEmptyIcon);
	private JLabel agreeLabel = new LinkLabel("同意条款");

	static {
		try {
			// 初始化config.ini配置文件
			iniEditor.load(new InputStreamReader(LoginFrame.class.getClass().getResourceAsStream("/config/config.ini")));
			style = iniEditor.get("frameSet", "style");
		} catch (

		IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建顶部面板
	 */
	private JPanel topPanel = new JPanel() {
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.drawImage(topleft, 0, 0, null);
			g2d.setPaint(topcPaint);
			g2d.drawImage(topleft, 0, 0, null);
			int x = (topleft != null) ? topleft.getWidth(null) : 10;
			int y = 0;
			g2d.fillRect(x, y, 334, 29);
			g2d.drawImage(topright, 344, 0, null);
		}
	};

	/**
	 * 最小化的按钮
	 */
	private Image miniImgTwo = ImageManager.getImage(iniEditor.get(style, "miniImgTwo"));
	private ImageIcon minButtonIcon = ImageManager.getImageIcon(iniEditor.get(style, "minButtonIcon"));
	private ImageIcon minButtonRoverIcon = ImageManager.createIcon(this.miniImgTwo, 0, 0, 18, 22);
	private ImageIcon minButtonPressedIcon = ImageManager.createIcon(this.miniImgTwo, 18, 0, 26, 22);
	private JButton minButton = new TransparentButton(this.minButtonIcon, this.minButtonRoverIcon, this.minButtonPressedIcon);

	/**
	 * 间隔的按钮
	 */
	private JButton splitButton = new TransparentButton(ImageManager.getImageIcon(iniEditor.get(style, "splitButton")));
	/**
	 * 关闭图标的按钮信息
	 */
	private Image closeImgTwo = ImageManager.getImage(iniEditor.get(style, "closeImgTwo"));
	private ImageIcon closeButtonIcon = ImageManager.getImageIcon(iniEditor.get(style, "closeButtonIcon"));
	private ImageIcon closeButtonRoverIcon = ImageManager.createIcon(this.closeImgTwo, 0, 0, 36, 22);
	private ImageIcon closeButtonPressedIcon = ImageManager.createIcon(this.closeImgTwo, 36, 0, 36, 22);
	private JButton closeButton = new TransparentButton(this.closeButtonIcon, this.closeButtonRoverIcon, this.closeButtonPressedIcon);

	/**
	 * 左侧的线条
	 */
	private JPanel leftlinePanel = new JPanel() {
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.setPaint(bglinePaint);
			g2d.fillRect(0, 0, 1, 175);
		}

	};

	/**
	 * 登录界面的主要窗体
	 */
	private JPanel mainPanel = new JPanel() {
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.setPaint(mainbgPaint);
			g2d.fillRect(0, 0, 350, 100);
		}

	};

	private JPanel rightlinePanel = new JPanel() {
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.setPaint(bglinePaint);
			g2d.fillRect(0, 0, 1, 175);
		}

	};

	/**
	 * 创建底部的面板
	 */
	private JPanel bottomPanel = new JPanel(new BorderLayout()) {
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.drawImage(bottomleft, 0, 0, null);
			g2d.setPaint(bottomcPaint);
			g2d.drawImage(bottomleft, 0, 0, null);
			int x = (bottomleft != null) ? bottomleft.getWidth(null) : 10;
			int y = 0;
			g2d.fillRect(x, y, 334, 39);
			g2d.drawImage(bottomright, 344, 0, null);
		}

	};

	private MouseAdapter moveWindowListener = new MouseAdapter() {
		private Point lastPoint;

		@Override
		public void mousePressed(MouseEvent e) {
			this.lastPoint = e.getLocationOnScreen();
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			Point point = e.getLocationOnScreen();
			int offsetX = point.x - this.lastPoint.x;
			int offsetY = point.y - this.lastPoint.y;
			Rectangle bounds = getBounds();
			bounds.x += offsetX;
			bounds.y += offsetY;
			setBounds(bounds);
			this.lastPoint = point;
		}
	};

	/**
	 * 
	 * Creates a new instance of LoginFrame. 初始化LoginFrame
	 */
	public LoginFrame() {
		init();
	}

	public void init() {

		final JPanel contentPanel = new JPanel();
		setUndecorated(true);

		contentPanel.setBackground(Color.WHITE);
		contentPanel.setLayout(null);
		contentPanel.addMouseListener(this.moveWindowListener);
		contentPanel.addMouseMotionListener(this.moveWindowListener);
		setIconImage(this.frame_ico);
		setTitle(this.frame_title);
		this.topPanel.setBounds(0, 0, 354, this.topheight);
		this.topPanel.setLayout(null);

		this.logoButton.setBounds(8, 5, 16, 16);
		this.logoText.setBounds(28, 3, 150, 20);
		this.topPanel.add(this.logoText);
		this.topPanel.add(this.logoButton);
		contentPanel.add(this.topPanel);

		this.minButton.setBounds(297, 0, 18, 22);
		this.minButton.setToolTipText("最小化");
		this.minButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setExtendedState(JFrame.ICONIFIED);
			}

		});
		this.topPanel.add(this.minButton);
		this.splitButton.setBounds(316, 0, 2, 22);
		this.topPanel.add(this.splitButton);
		this.closeButton.setBounds(317, 0, 36, 22);
		this.closeButton.setToolTipText("关闭");
		this.closeButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		this.topPanel.add(this.closeButton);

		this.leftlinePanel.setBounds(0, this.topheight, 1, 175);
		contentPanel.add(this.leftlinePanel);

		this.bannerPanel.setBounds(2, this.topheight, 350, 70);
		contentPanel.add(this.bannerPanel);

		this.processPanelDynamic.setBounds(2, this.topheight + 70, 39, 4);
		contentPanel.add(this.processPanelDynamic);

		this.processPanel.setBounds(2, this.topheight + 70, 350, 4);
		contentPanel.add(this.processPanel);

		this.mainPanel.setBounds(2, 103, 350, 100);
		this.mainPanel.setLayout(null);

		this.hintText.setBounds(11, 0, 108, 18);
		this.hintText.setFont(this.commonTxtFont);
		this.mainPanel.add(this.hintText);

		this.userIDLabel.setBounds(11, 22, 39, 18);
		this.userIDLabel.setFont(this.commonTxtFont);
		this.mainPanel.add(this.userIDLabel);

		this.userIDField.setBorder(BorderFactory.createLineBorder(this.idBordercolor));
		this.userIDField.setBounds(55, 22, 228, 20);
		this.userIDField.setFont(this.commonTxtFont);
		this.mainPanel.add(this.userIDField);

		this.pwdLable.setBounds(11, 50, 39, 18);
		this.pwdLable.setFont(this.commonTxtFont);
		this.mainPanel.add(this.pwdLable);

		this.pwdField.setEchoChar('*');
		this.pwdField.setBorder(BorderFactory.createLineBorder(this.pwdBordercolor));
		this.pwdField.setFont(this.commonTxtFont);
		this.pwdField.setBounds(55, 48, 228, 20);
		this.mainPanel.add(this.pwdField);
		this.getUserID.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ImageManager.OpenUri("http://freeqqm.qq.com/");
			}

		});
		this.getUserID.setBounds(289, 22, 49, 18);
		this.mainPanel.add(this.getUserID);
		this.getUserPwd.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ImageManager.OpenUri("http://service.qq.com/psw/mo.shtml?psw_cs.htm");
			}

		});
		this.getUserPwd.setBounds(289, 50, 60, 18);
		this.mainPanel.add(this.getUserPwd);

		this.remCheckBox.setBounds(47, 74, 65, 18);
		this.remCheckBox.setText("记住密码");
		this.mainPanel.add(this.remCheckBox);

		this.retreatCheckBox.setText("隐身登录");
		this.retreatCheckBox.setBounds(130, 74, 65, 18);
		this.mainPanel.add(this.retreatCheckBox);

		this.agreeCheckBox.setSelected(true);
		this.agreeCheckBox.setBounds(214, 74, 17, 18);
		this.mainPanel.add(this.agreeCheckBox);
		this.agreeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ImageManager.OpenUri("http://minigame.qq.com/new/intro/confer.html");
			}

		});
		this.agreeLabel.setBounds(232, 74, 49, 18);
		this.mainPanel.add(this.agreeLabel);

		contentPanel.add(this.mainPanel);

		this.rightlinePanel.setBounds(353, this.topheight, 1, 175);
		contentPanel.add(this.rightlinePanel);

		this.bottomPanel.setBounds(0, 204, 354, 39);
		this.bottomPanel.setLayout(null);
		this.setButton.setBounds(15, 10, 76, 23);
		this.bottomPanel.add(this.setButton);
		this.loginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		this.loginButton.setFont(new Font("宋体", 0, 12));
		this.loginButton.setBounds(265, 10, 76, 22);
		this.bottomPanel.add(this.loginButton);
		contentPanel.add(this.bottomPanel);

		this.setContentPane(contentPanel);
		this.setSize(this.width, 243);
		this.setLocationRelativeTo(null);

	}
}
